import {
  uploadFile,
  getFiles,
  getFileById,
  deleteFileById,
} from "../../api/file-upload.js";
import { verifyToken } from "../../utils/TokenCheck.js";
import { upload, createUploader } from "../../FileSystem/image-processor.js";

export default function fileUploadRouter(router) {
  // 上传文件接口
  router.post("/upload", verifyToken, upload.single("file"), uploadFile);
  // 动态目录上传接口示例1 - 用户头像
  router.post(
    "/upload/avatar",
    verifyToken,
    createUploader({
      baseDir: "uploads/avatars",
      //  自定义命名器
      filenameGenerator: (req, file, ext) => {
        const userId = req.headers["x-user-id"];
        return `${userId}${ext}`;
      },
    }).single("file"),
    uploadFile
  );

  router.post(
    "/upload/teachImg",
    verifyToken,
    createUploader({
      baseDir: "uploads/teachImg",
      //  自定义命名器
      filenameGenerator: (req, file, ext) => {
        const userId = req.headers["x-user-id"];
        return `${userId}${ext}`;
      },
    }).single("file"),
    uploadFile
  );


  // 获取文件列表
  router.get("/files", verifyToken, getFiles);

  // 获取单个文件信息
  router.get("/files/:id", verifyToken, getFileById);

  // 删除文件
  router.delete("/files/:id", verifyToken, deleteFileById);

  // 注意：文件下载不需要单独路由，直接通过静态文件访问
  // 例如：/uploads/filename.jpg
}
